package role

import (
	"context"

	"mall/common/errorx"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateRoleMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateRoleMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRoleMenuLogic {
	return &UpdateRoleMenuLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateRoleMenuLogic) UpdateRoleMenu(req *types.UpdateRoleMenuRequest) (resp *types.UpdateRoleMenuResponse, err error) {
	if req.RoleId == 0 {
		return nil, errorx.Error(errorx.ERROR_ROLE_ID_REQUIRED, "")
	}

	if len(req.MenuIds) == 0 {
		return nil, errorx.Error(errorx.ERROR_MENU_ID_REQUIRED, "")
	}

	_, err = l.svcCtx.BackendRpc.BackendUpdateRole(l.ctx, &backendclient.UpdateRoleRequest{
		Id:      req.RoleId,
		MenuIds: req.MenuIds,
	})
	if err != nil {
		return nil, err
	}

	return &types.UpdateRoleMenuResponse{
		Status:  "ok",
		Message: "更新成功",
	}, nil
}
